package de.g18.it0a.jlaser.domain;

import java.util.Vector;


public class LaserConfiguration {

    public static final LaserConfiguration EMPTY = new LaserConfiguration(new Position(0, 0), new Vector<Mirror>(), new Position(0, 0));

    private final Position start;
    private final Position end;
    private final Vector<Mirror> mirrors;


    public LaserConfiguration(Position aStart, Position aEnd, Vector<Position> aMirrorPositions) {
        this(aStart, createMirrors(aMirrorPositions, aEnd), aEnd);
    }

    private static Vector<Mirror> createMirrors(Vector<Position> aMirrorPositions, Position aEnd) {
        Vector<Mirror> mirrors = new Vector<Mirror>();
        Mirror lastMirror = null;
        for (Position mirrorPosition : aMirrorPositions) {
            Mirror mirror = new Mirror(mirrorPosition);
            mirrors.add(mirror);

            if (lastMirror != null) {
                lastMirror.setPointedPosition(mirrorPosition);
            }
            lastMirror = mirror;
        }
        if (lastMirror != null) {
            lastMirror.setPointedPosition(aEnd);
        }
        return mirrors;
    }

    private LaserConfiguration(Position aStart, Vector<Mirror> aMirrors, Position aEnd) {
        start = aStart;
        mirrors = aMirrors;
        end = aEnd;
    }

    public Position getStart() {
        return start;
    }

    public Position getEnd() {
        return end;
    }

    public Vector<Mirror> getMirrors() {
        return mirrors;
    }

    public Vector<Position> getMirrorPositions() {
        Vector<Position> result = new Vector<Position>();
        for (Mirror mirror : getMirrors()) {
            result.add(mirror.getPosition());
        }
        return result;
    }
}
